﻿using System;
using System.Windows.Forms;
using EntitiesContainer;
using Shop.Interfaces;
using System.Collections.Generic;
using System.Drawing;

namespace Shop
{
    public partial class MainForm : Form, IViewMain
    {
        public MainForm()
        {
            InitializeComponent();
            cbx_Category.SelectedIndexChanged += (sender, args) => Invoke(CategorySelected);
            dgv_Table.DoubleClick += (sender, args) =>
            {
                if (((DataGridView)sender).SelectedRows != null)
                {
                    Invoke(RowSelected);
                }
            };
            cbx_CategoryProd.Click += (sender, args) => Invoke(ComboboxProductDropDown);
            btn_Logout.Click += (sender, args) => Invoke(LogoutClicked);
            btn_Add.Click += (sender, args) => Invoke(AddButtonClicked);
            btn_Change.Click += (sender, args) => Invoke(ChangeButtonClicked);
            btn_Delete.Click += (sender, args) => Invoke(DelButtonClicked);
        }

        public event Action CategorySelected;
        public event Action RowSelected;
        public event Action ComboboxProductDropDown;
        public event Action LogoutClicked;
        public event Action AddButtonClicked;
        public event Action ChangeButtonClicked;
        public event Action DelButtonClicked;

        public ICollection<Product> tableProducts
        {
            set
            {
                dgv_Table.DataSource = value;
            }
        }
        public string UserName
        {
            set
            {
                lbl_Username.Text = value;
            }
        }
        public string ProdName
        {
            get
            {
                return tbx_Product.Text;
            }
            set
            {
                tbx_Product.Text = value;
            }
        }
        public string ProductCategoryFromProdInfo
        {
            get
            {
                return cbx_CategoryProd.Text;
            }
            set
            {
                cbx_CategoryProd.Text = value;
            }
        }
        public ICollection<string> ProductCategoriesFromTable
        {
            get
            {
                return (ICollection<string>)cbx_Category.DataSource;
            }
            set
            {
                cbx_Category.DataSource = value;
            }
        }

        public string Price
        {
            get
            {
                return tbx_Price.Text;
            }
            set
            {
                tbx_Price.Text = value;
            }
        }
        public string Description
        {
            get
            {
                return tbx_Description.Text;
            }
            set
            {
                tbx_Description.Text = value;
            }
        }
        public Image ProductImage
        {
            get
            {
                return pic_Product.Image;
            }
            set
            {
                pic_Product.Image = value;
            }
        }

        //public void SetListCategoriesForTable(ICollection<string> categories)
        //{
        //    cbx_Category.DataSource = categories;            
        //    cbx_Category.SelectedIndex = 0;
        //}
        public void SetListCategoriesForProduct(ICollection<string> categories)
        {
            cbx_CategoryProd.DataSource = categories;
            //cbx_CategoryProd.SelectedIndex = 0;
        }
        public string ReturnSelectedCategoryTable()
        {
            return cbx_Category.Text;
        }
        public string ReturnSelectedCategoryProd()
        {
            return cbx_CategoryProd.Text;
        }
        public int ReturnSelectedProductId()
        {
            return Convert.ToInt32(dgv_Table.SelectedRows[0].Cells[0].Value);
        }
        private void btn_ImageUpload_Click(object sender, EventArgs e)
        {
            if (ofd_LoadImage.ShowDialog() == DialogResult.OK)
            {
                pic_Product.Image = Image.FromFile(ofd_LoadImage.FileName);
            }
        }
        private void Invoke(Action action)
        {
            if (action != null)
            {
                action();
            }
        }
        public new void Show()
        {
            Application.Run(this);
        }
    }
}
